package com.wakala.employment.adapter.jobCategory;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.wakala.employment.R;
import com.wakala.employment.adapter.base.FlowTagSimpleAdapter;
import com.xuexiang.xui.utils.XToastUtils;
import com.xuexiang.xui.widget.flowlayout.FlowTagLayout;
import com.xuexiang.xutil.tip.ToastUtils;

import java.util.List;


public class ClassifyDetailAdapter extends RvAdapter<JobCategoryTreeVo> {

    private TagSelectListener tagSelectListener;

    public ClassifyDetailAdapter(Context context, List<JobCategoryTreeVo> list, RvListener listener) {
        super(context, list, listener);
    }
    public ClassifyDetailAdapter(Context context, List<JobCategoryTreeVo> list, RvListener listener,TagSelectListener tagSelectListener) {
        super(context, list, listener);
        this.tagSelectListener = tagSelectListener;
    }


    @Override
    protected int getLayoutId(int viewType) {
        return viewType == 0 ? R.layout.item_job_category_detail_title : R.layout.item_job_category_detail;
    }

    @Override
    public int getItemViewType(int position) {
        return  1;
    }

    @Override
    protected RvHolder getHolder(View view, int viewType) {
        return new ClassifyHolder(view, viewType, listener);
    }

    public class ClassifyHolder extends RvHolder<JobCategoryTreeVo> {
        TextView tvCity;
        ImageView avatar;
        TextView tvTitle;

        FlowTagLayout mTagContainerLayout;

        public ClassifyHolder(View itemView, int type, RvListener listener) {
            super(itemView, type, listener);
            switch (type) {
                case 0:
                    tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
                    break;
                case 1:
                    tvCity = (TextView) itemView.findViewById(R.id.tvCity);
                    avatar = (ImageView) itemView.findViewById(R.id.ivAvatar);
                    mTagContainerLayout = itemView.findViewById(R.id.flowlayout_multi_select);
                    break;
            }

        }

        @Override
        public void bindHolder(JobCategoryTreeVo sortBean, int position) {
            int itemViewType = ClassifyDetailAdapter.this.getItemViewType(position);
            switch (itemViewType) {
                case 0:
                    tvTitle.setText(sortBean.getName());
                    break;
                case 1:
                    tvCity.setText(sortBean.getName());
                    if(null!=sortBean.getChild() && !sortBean.getChild().isEmpty()){
                        mTagContainerLayout.setTag(sortBean.getId());
                        FlowTagSimpleAdapter tagAdapter = new FlowTagSimpleAdapter<JobCategoryTreeVo>(mTagContainerLayout.getContext());
                        mTagContainerLayout.setAdapter(tagAdapter);
                        mTagContainerLayout.setTagCheckedMode(FlowTagLayout.FLOW_TAG_CHECKED_MULTI);//可多选
                        for(JobCategoryTreeVo vo:sortBean.getChild()){
                            tagAdapter.addTag(vo);
                        }
                        mTagContainerLayout.setOnTagSelectListener(new FlowTagLayout.OnTagSelectListener() {
                            @Override
                            public void onItemSelect(FlowTagLayout flowTagLayout, int i, List<Integer> list) {
                                tagSelectListener.onItemSelect( flowTagLayout,  i,  list);
                            }
                        });
                    }else {
                        mTagContainerLayout.clearTags();
                    }

                    break;
            }

        }
    }
}
